package com.syf.core.service.context;

import com.syf.core.engine.SyFrameContext;
import com.syf.core.service.SyService;
import com.syf.core.service.invoke.SyServiceInvoker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * T为请求数据体对象
 * R为响应数据体对象
 */

public class ServiceContext<T, R> {
    private final ServiceRequest<T> request;

    private final ServiceResponse<R> response;

    private final Logger traceLog;

    private final SyFrameContext frameCtx;

    public ServiceContext(String callingAppCompName, T reqDataObj, SyFrameContext frameCtx) {
        this(new ServiceRequest<>(callingAppCompName, reqDataObj), frameCtx);
    }

    public ServiceContext(ServiceRequest<T> request, SyFrameContext frameCtx) {
        this(request, new ServiceResponse<>(), frameCtx);
    }

    public ServiceContext(ServiceRequest<T> request, ServiceResponse<R> response, SyFrameContext frameCtx) {
        this.request = request;
        this.response = response;
        this.frameCtx = frameCtx;
        this.traceLog = LoggerFactory.getLogger(SyService.class);
    }

    public ServiceRequest<T> getRequest() {
        return request;
    }

    public ServiceResponse<R> getResponse() {
        return response;
    }

    /**
     * 获取工具库对象
     */
    public <D> D getToolKit(Class<D> kitClazz) {
        return frameCtx.getToolKit(kitClazz);
    }

    /**
     * 获取链路日志对象
     */
    public Logger getTraceLog() {
        return traceLog;
    }

    /**
     * 获取本地service invoker
     */
    public SyServiceInvoker getLocalInvoker() {
        return frameCtx.getServiceInvoker();
    }

    /**
     * 获取系统配置参数值
     */
    public String getAppConfigProperty(String propertyName) {
        return frameCtx.getProperty(propertyName);
    }
}